package com.fafu.service.Impl;

import com.fafu.dao.mapper.ImageMapper;
import com.fafu.dao.mapper.StudentMapper;
import com.fafu.dao.mapper.StudentrepairMapper;
import com.fafu.model.Image;
import com.fafu.model.Student;
import com.fafu.model.Studentrepair;
import com.fafu.pojo.AppPojo4Item;
import com.fafu.pojo.AppPojo4list;
import com.fafu.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import util.Dictionary;
import util.MyMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static util.Dictionary.TABLE.TABLE_STUDENT_REPAIR;

@Service
public class AppServiceImpl implements AppService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentrepairMapper studentrepairMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Override
    public Map<String, Object> selectRecordList(Map<String, Object> paramMap) {
        Student student = studentMapper.selectByPrimaryKey(paramMap.get("id"));
        List<AppPojo4list> reList = studentrepairMapper.appSelectRecordList(
                new MyMap().put("studentAddr",student.getStudentAddr())
                        .put("state",paramMap.get("type")).toMap());
        Example example;
        for(AppPojo4list data:reList){
            example = new Example(Image.class);
            example.and().andEqualTo("tableIden",TABLE_STUDENT_REPAIR).andEqualTo("repairid",data.getId());
            List<Image> images = imageMapper.selectByExample(example);
            if(images != null && images.size()>0)
                data.setPic(images.get(0).getUrl());
        }
        return new MyMap().put("data",reList).toMap();
    }

    @Override
    public Map<String, Object> selectRecordItem(Map<String, Object> paramMap) {
        AppPojo4Item record = studentrepairMapper.appSelectRecordItem(paramMap);
        Example example = new Example(Image.class);
        example.and().andEqualTo("repairid",record.getId()).andEqualTo("tableIden",TABLE_STUDENT_REPAIR);
        List<Image> imagesList = imageMapper.selectByExample(example);
        List<String> urlList = new ArrayList<>();
        for(Image data:imagesList){
            urlList.add(data.getUrl());
        }
        record.setPicList(urlList);
        record.setTitle(Dictionary.TITLE.getTitle(record.getState()));
        return new MyMap().put("data",record).toMap();
    }

    @Transactional
    @Override
    public Map<String, Object> updateRecordItem(Map<String, Object> paramMap) {
        Integer id = (Integer) paramMap.get("id");
        Studentrepair studentrepair = studentrepairMapper.selectByPrimaryKey(id);
        if(studentrepair.getState() != Dictionary.REPAIR.REPAIR_STATE_NO_READ)
            return new MyMap().put("code",100).put("msg","报修状态已修改").toMap();
        studentrepair.setMsg((String) paramMap.get("msg"));
        studentrepairMapper.updateByPrimaryKeySelective(studentrepair);
        return new MyMap().put("code",200).toMap();
    }

    @Override
    public String selectAddr(String studentId) {
        Example example = new Example(Student.class);
        example.and().andEqualTo("studentIden",studentId);
        List<Student> students = studentMapper.selectByExample(example);
        if(students == null || students.size() == 0)
            return "-1";
        return students.get(0).getStudentAddr();
    }
}
